1877D - Effects of Anti Pimples - CodeForces Solution


combinatorics number theory sortings

Please click on ads to support us..

C++ Code:

#include "bits/stdc++.h"
using namespace std;
#define int               long long
#define ll               long long
#define uniq(v)           (v).erase(unique(all(v)),(v).end())

//const long long INF=1e18;
//const int32_t M=1e9+7;
const int32_t MM=998244353;
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
void solve()
{
	int n;
	cin>>n;
	vector<int> nums(n);
	for(int i=0;i<n;i++)
	{
		cin>>nums[i];
	}
	vector<int> v(n);
	for(int i=0;i<n;i++)
	{
		int ma=INT_MIN;
		for(int j=i;j<n;j=j+(i+1))
		{
			ma=max(ma,nums[j]);
		}
		v[i]=ma;

	}


	

	// for(int i=0;i<n;i++)
	// {
	// 	cout<<v[i]<<" ";
	// }
	int ans=0;
	sort(v.begin(),v.end());

	for(int i=0;i<n;i++)
	{
		int t=expo(2,i,MM);
		int g=mod_mul(v[i],t,MM);

		int k=mod_add(g,ans,MM);
		//cout<<t<<" "<<g<<" "<<k<<endl;
		ans=k;
	}
	cout<<ans<<endl;


}
signed main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	#ifdef SIEVE
		sieve();
	#endif
	#ifdef NCR
		init();
	#endif
	int t=1;
	//cin>>t;
	while(t--) solve();
	return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST